package huq.com.io;

import huq.com.common.*;

import java.io.File;
import java.io.IOException;


import jxl.NumberCell;
import jxl.Sheet;
import jxl.Workbook;
import jxl.write.Label;
import jxl.write.Number;
import jxl.write.WritableSheet;
import jxl.write.WritableWorkbook;
import jxl.write.WriteException;
import jxl.write.biff.RowsExceededException;

/**
 * Modifications by Qian:
 * 	1. remove all "static"
 * 
 * input 
 * @author Xiang
 *
 */

public class InXLS {
	Workbook workbook;
	int shs;	// # of sheets
	int line;
	
	public void open(String filename){
		try {
			workbook = Workbook.getWorkbook(new File(filename));
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}
	
	public String[] getItemIDs(){
		Sheet sheet = workbook.getSheet(2);
		int M = sheet.getRows();
		String[] itemIDs = new String[M];
		for(int i=0;i<M;i++){
			itemIDs[i] = sheet.getCell(0,i).getContents();
		}
		return itemIDs;
	}
	
	public Sheet getSheet(int sheetNO){
		Sheet sheet = workbook.getSheet(sheetNO);
		return sheet;
	}
	
	public int getNumSheets(){
		return workbook.getNumberOfSheets();
	}
	
	public void getShipper(Shipper shipper, Instance inst){
		Sheet sheet = workbook.getSheet(0);
		int M = sheet.getRows();
		int I = 0;
		for(int i = 1; i < M; i++){
			String line = sheet.getCell(0, i).getContents();
			String sc = line.trim();
			
			line = sheet.getCell(1, i).getContents();
			String dc = line.trim();
			
			line = sheet.getCell(2, i).getContents();
			String et = line.trim();
			
			int shipment = (int)((NumberCell)sheet.getCell(3, i)).getValue();
			
			Lane lane = new Lane(I++, sc, dc, et, shipment);
			inst.lanes.add(lane);
			shipper.addSLane(lane, shipment);
		}
	}
	
	public void getCarrier(Carrier carrier, Instance inst){
		Sheet sheet = workbook.getSheet(0);
		int M = sheet.getRows();
		for(int i = 1; i < M; i++){
			String line = sheet.getCell(0, i).getContents();
			String sc = line.trim();
			
			line = sheet.getCell(1, i).getContents();
			String dc = line.trim();
			
			line = sheet.getCell(2, i).getContents();
			String et = line.trim();
			
			int rate = (int)((NumberCell)sheet.getCell(3, i)).getValue();
			
			int capacity = (int)((NumberCell)sheet.getCell(4, i)).getValue();
			
			Lane lane = inst.getLane(sc, dc, et);
			if (lane == null){
				System.out.println(carrier.name);
				System.out.println(sc + ',' + dc + ',' + et);
			}
			carrier.addCLane(lane,rate,capacity);
		}
	}
	
	public static void writeSolution(String name, Solution solution){
		if(solution == null){
			System.out.println("Solution is null!");
			return;
		}
		System.out.println("TotalCost " + solution.totalCost);
		System.out.println("gap " + solution.gap);
		System.out.println("Status " + solution.status);
		System.out.println("Time " + solution.time);
		try {
			int i, j;
			WritableWorkbook workbook = Workbook.createWorkbook(new File(name + ".xls"));
			WritableSheet sheet = workbook.createSheet("Result", 0);
			Label label = new Label(0, 0, "Total Cost"); 
			sheet.addCell(label); 
			Number number = new Number(1, 0, solution.totalCost); 
			sheet.addCell(number); 
			i = 1;
			for(Solution.LaneSolution ls : solution.laneSolutions){
				label = new Label(0, i, ls.lane.sourceCity); 
				sheet.addCell(label);
				label = new Label(1, i, ls.lane.destCity); 
				sheet.addCell(label);
				label = new Label(2, i, ls.lane.equipType); 
				sheet.addCell(label);
				number = new Number(3, i, ls.lane.shipment); 
				sheet.addCell(number); 
				j = 4;
				for(int k = 0; k < ls.M; k++){
					label = new Label(j, i, ls.carriers[k].name); 
					sheet.addCell(label);
					j++;
					number = new Number(j, i, ls.shipments[k]); 
					sheet.addCell(number); 
					j++;
				}
				i++;
			}
			workbook.write(); 
			workbook.close(); 
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (RowsExceededException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (WriteException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}
	
	public void close(){
		try{
			workbook.close();
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}
}
